#coding:utf8

import time

from socket import AF_INET,SOCK_STREAM,socket
from thread import start_new
import struct
import Role_pb2

HOST='localhost'
PORT=34530
BUFSIZE=1024
ADDR=(HOST , PORT)
client = socket(AF_INET,SOCK_STREAM)
client.connect(ADDR)

def sendData(sendstr,commandId):
    HEAD_0 = chr(0)
    HEAD_1 = chr(0)
    HEAD_2 = chr(0)
    HEAD_3 = chr(0)
    ProtoVersion = chr(0)
    ServerVersion = 0
    sendstr = sendstr
    data = struct.pack('!sssss3I',HEAD_0,HEAD_1,HEAD_2,\
                       HEAD_3,ProtoVersion,ServerVersion,\
                       len(sendstr)+4,commandId)
    senddata = data+sendstr
    print(senddata.__len__())
    return senddata

request = Role_pb2.CreatePlayerRequest();
request.name = 'zhangsan'
request.password = '123'
byte = request.SerializeToString();
client.sendall(sendData(byte,1))

while True:
    pass

# s1 = time.time()
#
# def start():
#     for i in xrange(10):
#         client.sendall(sendData('asdfe',1))
#
# for i in range(10):
#     start_new(start,())

